package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import top.infopub.dto.bpa.OtherContractsInventoryDto;
import top.infopub.dto.bpa.OtherContractsReplenishDto;
import top.infopub.dto.bpa.OtherContractsReviewDto;
import top.infopub.mgr.bpa.domain.OtherConInventoryDomain;
import top.infopub.mgr.bpa.service.OtherContractsInventoryService;
import top.infopub.mgr.bpa.vo.OtherConInventoryVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.model.bpa.OtherContractsInventory;
import top.infopub.model.bpa.OtherContractsReview;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 其他合同清单录 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-18
 */
@Controller
@RequestMapping("/contractsInventory")
public class OtherContractsInventoryController {
    @Autowired
    OtherContractsInventoryService otherContractsInventoryService;
    @Autowired
    private OAFlowService oaFlowService;
    /**
     * 其他合同清单录入列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<OtherConInventoryDomain> queryDataByCondition(OtherConInventoryVo otherConInventoryVo,
                                                               ParamPage paramPage) throws ParseException {
        Page<OtherConInventoryDomain> page = new Page<OtherConInventoryDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = otherContractsInventoryService.selectPageByDomainWhere(page, otherConInventoryVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    //下拉框数据
    @RequestMapping("/projectDropDownBox")
    @ResponseBody
    public List<Map<String, String>> projectDropDownBox() {
        return otherContractsInventoryService.projectDropDownBox();
    }
    //详情
    @RequestMapping("/queryById")
    @ResponseBody
    public OtherContractsInventoryDto queryById(String id) {
        OtherContractsInventoryDto dao = otherContractsInventoryService.queryById(id);
        return dao;
    }
    /*跳转*/
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id) {
        request.setAttribute("id",id);
        return "bpa/lay-OtherContractList-detail";
    }

    // 保存提交
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, String submitType, String proCode,String id) {
        if (StringUtils.isEmpty(bodyStr)) {
            return RspVo.getFailureResponse(0, "数据为空");
        }
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String object = StringEscapeUtils.unescapeJava(bodyStr);
        OtherContractsInventoryDto dto = JSON.parseObject(object, OtherContractsInventoryDto.class);
        if ("1".equals(submitType)) {//1是保存
            int i = otherContractsInventoryService.save(dto);
            if (i > 0) {
                return RspVo.getSuccessResponse(0, "保存成功");
            } else {
                return RspVo.getFailureResponse(0, "保存失败");
            }
        } else { //2是提交

            //判断状态
            OtherContractsInventory otherContractsInventory  = otherContractsInventoryService.findById(id);
            if (otherContractsInventory!=null)
            {
                String processStatus = otherContractsInventory.getProcessStatus();
                if ("1".equals(processStatus) || "9".equals(processStatus))
                {
                    return RspVo.getFailureResponse(0, "审批中或审批已完成，修改失败");
                }
            }

            if (StringUtils.isNotBlank(otherContractsInventory.getOaId())) {
                oaFlowService.finishOA(otherContractsInventory.getOaId());
            }
            //提交流程  先保存
            int i = otherContractsInventoryService.save(dto);
            if (i > 0) {//保存成的情况 走流程
                //发起流程
                /*String process = proProcessManageService.sendProcess(ProProcessDetailTypeEnum.PRO_NODE_TYPE15, object, id, code, vo);*/
                return RspVo.getFailureResponse(0, "提交成功");
            } else { //提交-保存失败
                return RspVo.getFailureResponse(0, "提交失败");
            }
        }
    }
}
